package com.iotechn.unimall.admin.api.file;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.FileDO;
import com.iotechn.unimall.data.domain.FileDO;
import com.iotechn.unimall.data.domain.ImgDO;
import com.iotechn.unimall.data.dto.FileDTO;
import com.iotechn.unimall.data.dto.goods.FileInfo;
import com.iotechn.unimall.data.dto.goods.SpuDTO;
import com.iotechn.unimall.data.enums.BizType;
import com.iotechn.unimall.data.mapper.FileMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class AdminFileServiceImpl implements AdminFileService {
    @Autowired
    private FileMapper fileMapper;

    @Override
    public Page<FileDO> list(Long  shopId,Long  jiedianId,  String title, Integer page, Integer limit, Long adminId) throws ServiceException {
        Wrapper<FileDO> wrapper = new EntityWrapper<FileDO>();
        if(shopId!=null){
            wrapper.eq("shop_id",shopId);
        }
        if(jiedianId!=null){
            wrapper.eq("jiedian_id",jiedianId);
        }
        if(!StringUtils.isEmpty(title)){
            wrapper.like("name",title);
        }
        wrapper.orderBy("id", false);
        Integer count = fileMapper.selectCount(wrapper);
        List<FileDO> FileDOS = fileMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(FileDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(FileDTO FileDO, Long adminId) throws ServiceException {
        FileDO.setAdminId(adminId);
        Date now = new Date();
        FileDO.setGmtCreate(now);
        FileDO.setGmtUpdate(now);
       return insertFile( FileDO,  now);
     }

    private Boolean insertFile(FileDTO fileDTO, Date now) {
        List<FileInfo> imgList = fileDTO.getFileList();
        if(imgList!=null && imgList.size()>0){
            List<FileDO> imgDOList = imgList.stream().map(item -> {
                FileDO imgDO = new FileDO();
                imgDO.setBizType(BizType.SHOP.getCode());
                imgDO.setBizId(fileDTO.getShopId());
                imgDO.setUrl(item.getUrl());
                imgDO.setName(item.getName());
                imgDO.setGmtCreate(now);
                imgDO.setGmtUpdate(now);
                imgDO.setJiedianId(fileDTO.getJiedianId());
                imgDO.setJiedianName(fileDTO.getJiedianName());
                imgDO.setShopId(fileDTO.getShopId());
                imgDO.setShopName(fileDTO.getShopName());
                return imgDO;
            }).collect(Collectors.toList());
            fileMapper.insertFile(imgDOList);
        }
        return true;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(FileDO FileDO, Long adminId) throws ServiceException {
        Long id = FileDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        FileDO.setGmtUpdate(now);
        if (fileMapper.updateById(FileDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (fileMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids)) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        for (Long spuId : ids) {
            fileMapper.deleteById(spuId);
        }
        return "ok";
    }


    @Override
    public FileDO detail(Long id, Long adminId) throws ServiceException {
        return fileMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        FileDO FileDO = new FileDO();
        FileDO.setId(id);
        FileDO.setGmtUpdate(new Date());
        if (fileMapper.updateById(FileDO) > 0) {
            return true;
        }
        return false;
    }

}